/**
 * JMPlurk, a Java Mobile client for Plurk
 * Copyright (C) 2009 Petra Barus
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package com.maleskoding.jmplurk.ui;

import com.maleskoding.jmplurk.JMPlurk;
import com.maleskoding.jmplurk.util.Preferences;
import com.sun.lwuit.Command;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.TextField;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import javax.microedition.rms.RecordStoreException;

/**
 *
 * @author Petra Barus
 */
public class SettingForm extends Form implements ActionListener {

    private JMPlurk parent;
    //
    private Command saveCommand;
    private Command backCommand;
    private Command exitCommand;
    //
    private Label usernameLabel;
    private TextField usernameField;
    private Label passwordLabel;
    private TextField passwordField;
    private String username;
    private String password;
    //
    private boolean isFirstTime;
    //
    private Preferences preferences;

    public SettingForm(JMPlurk parent) {
        super("Settings");
        this.parent = parent;
        init();
    }

    public void init() {
        //Adding labels and textfields
        usernameLabel = new Label("Username");
        usernameField = new TextField();
        passwordLabel = new Label("Password");
        passwordField = new TextField();
        passwordField.setConstraint(TextField.PASSWORD);

        this.addComponent(usernameLabel);
        this.addComponent(usernameField);
        this.addComponent(passwordLabel);
        this.addComponent(passwordField);
        //Adding commands
        backCommand = new Command("Back");
        exitCommand = new Command("Exit");
        saveCommand = new Command("Save");
        this.addCommand(saveCommand);
        this.setBackCommand(backCommand);
        this.addCommandListener(this);
    }

    /**
     *
     */
    public void loadFormSetting() {
        try {
            preferences = Preferences.getInstance();
            username = preferences.get("username");
            password = preferences.get("password");
            if (username == null || password == null) {
                throw new Exception();
            }
            isFirstTime = false;
        } catch (Exception ex) {
            isFirstTime = true;
            username = new String("");
            password = new String("");
        } finally {
            usernameField.setText(username);
            passwordField.setText(password);
            if (isFirstTime) {
                this.addCommand(exitCommand);
            } else {
                this.removeCommand(exitCommand);
                this.addCommand(backCommand);
            }
        }
    }

    /**
     * 
     */
    public void show() {
        loadFormSetting();
        super.show();
    }

    /**
     * 
     */
    public void saveSetting() {
        try {
            preferences.put("username", this.usernameField.getText());
            preferences.put("password", this.passwordField.getText());
            preferences.save();
        } catch (RecordStoreException ex) {
            //TODO: Handle failed
            ex.printStackTrace();
        }
    }

    /**
     *
     * @return
     */
    public Command getExitCommand() {
        return exitCommand;
    }

    /**
     * 
     * @return
     */
    public Command getSaveCommand() {
        return saveCommand;
    }

    public void actionPerformed(ActionEvent ae) {
        Command command = ae.getCommand();
        if (command == (saveCommand)) {
            if (usernameField.getText().length() == 0 || passwordField.getText().length() == 0) {
                Dialog.show("Error", "Username or password is still blank", "OK", null);
            } else {
                saveSetting();
                parent.getMainPlurkForm().show();
            }
        } else if (command == (exitCommand)) {
            parent.exitMIDlet();
        } else if (command == (backCommand)) {
            parent.getMainPlurkForm().show();
        }
    }
}
